package com.qzsf.backend.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qzsf.backend.common.Result;
import com.qzsf.backend.entity.UserAuth;
import com.qzsf.backend.mapper.UserAuthMapper;
import com.qzsf.backend.service.RoleService;
import com.qzsf.backend.util.Sha256Util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/*
 * @Author: Dennis Chen
 * @CreateTime: 2024/11/8 10:34 周五
 * @Email 1429278462@qq.com
 * @Remark 用户权限管理服务层实现类
 * @Version 1.0
 */
@Service
public class RoleServiceImpl implements RoleService {
    @Autowired
    private UserAuthMapper userAuthMapper;
    @Override
    public Result getUserAuth(String username, int pageNo, int pageSize) {
        PageHelper.startPage(pageNo, pageSize);
        if(username == null){
            return Result.success(new PageInfo<>(userAuthMapper.getAllUserAuth()));
        }else {
            return Result.success(new PageInfo<>(userAuthMapper.getUserAuthByName(username)));
        }
    }

    @Override
    public Result deleteUserAuth(int id) {
        if(userAuthMapper.deleteUserAuth(id)>0){
            return Result.success();
        }else{
            return Result.error("删除失败");
        }
    }

    @Override
    public Result updateUserAuth(UserAuth userAuth) {
        String sha256String = Sha256Util.getSHA256String(userAuth.getPassword());
        userAuth.setPassword(sha256String);
        if(userAuthMapper.updateUserAuth(userAuth)>0){
            return Result.success();
        }else{
            return Result.error("修改异常");
        }
    }

    @Override
    public Result createUserAuth(UserAuth userAuth) {
        String sha256String = Sha256Util.getSHA256String(userAuth.getPassword());
        userAuth.setPassword(sha256String);
        if(userAuthMapper.createUserAuth(userAuth)>0){
            return Result.success();
        }else{
            return Result.error("修改异常");
        }
    }

    @Override
    public Result getUserAuthById(int id) {
        return Result.success(userAuthMapper.getUserAuthById(id));
    }
}
